package com.rocksea.app.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mor.dataacquisition.DataAcquisition;
import com.mor.dataacquisition.net.DataCallBack;
import com.mor.dataacquisition.net.parsedData.ResponseData;
import com.rocksea.app.Account;
import com.rocksea.app.BaseFragment;
import com.rocksea.app.R;
import com.rocksea.app.activity.SInspecFromActivity;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.utils.HttpMessage;
import com.rocksea.app.utils.UiUtils;
import com.rocksea.app.weight.DividerListItemDecoration;

import java.util.ArrayList;

import butterknife.InjectView;

/**
 * @author Administrator
 * @date 2017/11/21
 */

public class SInspecFromFragment extends BaseFragment
{

    @InjectView(R.id.SInpecFrom_ListView)
    public RecyclerView mRecyclerView;
    private InspectionForm.InspectionFormListBean mFormListBean;

    public static SInspecFromFragment newInstance(Bundle args)
    {
        SInspecFromFragment fragment = new SInspecFromFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initView()
    {
        Bundle mBundle = getArguments();
        String pileNum = mBundle.getString("PileNum");
        mFormListBean = mBundle.getParcelable(UiUtils.BUNDLE_KEY);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        setHasOptionsMenu(true);  //注册菜单
    }

    @Override
    protected void initData()
    {
        loadCompleted();
        ArrayList<String> mInfos = init(mFormListBean);
        mRecyclerView.setAdapter(new AdapterDemo(mInfos,getContext()));
        mRecyclerView.addItemDecoration(new DividerListItemDecoration(getContext(),DividerListItemDecoration.VERTICAL_LIST));
    }

    @Override
    protected void requestAgain() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_sinspecfrom_layout;
    }

    private static class AdapterDemo extends RecyclerView.Adapter<AdapterDemo.ViewHolder> {

        private ArrayList<String> datas;
        private Context mContext;
        private AdapterDemo(ArrayList<String> datas,Context mContext) {
            this.datas = datas;
            this.mContext = mContext;
        }
        @Override
        public AdapterDemo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View item = LayoutInflater.from(mContext).inflate(R.layout.inspe_list_item, parent,false);
            return new AdapterDemo.ViewHolder(item);
        }

        @Override
        public void onBindViewHolder(AdapterDemo.ViewHolder holder, int position) {
            String index = datas.get(position);
            if (index.contains("|"))
            {
                String[] infos = index.split("\\|");
                if (infos[1].length() > 24)
                    holder.mInfo.setText(String.format("%s...%s",infos[1].substring(0,8),infos[1].substring(infos[1].length()-8)));
                else
                    holder.mInfo.setText(infos[1]);
                holder.mTitle.setText(infos[0]);
                holder.mInfo.setTextColor(Color.BLACK);
            }
        }
        @Override
        public int getItemCount() {
            return datas.size();
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTitle, mInfo;
            public ViewHolder(View view)
            {
                super(view);
                mTitle = (TextView) view.findViewById(R.id.Title);
                mInfo = (TextView) view.findViewById(R.id.Info);
            }
        }
    }
    private ArrayList<String> init(InspectionForm.InspectionFormListBean mFormListBean){
        ArrayList<String> mInfo = new ArrayList<>();
        mInfo.add(String.format("%s|%s","报检单编号",mFormListBean.number.equals("")?"未知":mFormListBean.number));
        mInfo.add(String.format("%s|%s","建筑物名称",mFormListBean.siteName.equals("")?"未知":mFormListBean.siteName));
        mInfo.add(String.format("%s|%s","标段名称",mFormListBean.sectName.equals("")?"未知":mFormListBean.sectName));
        mInfo.add(String.format("%s|%s","受理人",mFormListBean.receiver.equals("")?"未知":mFormListBean.receiver));
        mInfo.add(String.format("%s|%s","申请人",mFormListBean.applicantName.equals("")?"未知":mFormListBean.applicantName));
        mInfo.add(String.format("%s|%s","申请人电话",mFormListBean.applicantTel.equals("")?"未知":mFormListBean.applicantTel));
        mInfo.add(String.format("%s|%s","报检时间",mFormListBean.applyTime.equals("")?"未知":mFormListBean.applyTime));
        mInfo.add(String.format("%s|%s","混凝土强度等级",mFormListBean.concreteStrength.equals("")?"未知":"C"+mFormListBean.concreteStrength));
        mInfo.add(String.format("%s|%s","设计桩长",mFormListBean.desPileLength.equals("")?"未知":mFormListBean.desPileLength+"m"));
        mInfo.add(String.format("%s|%s","设计桩径",mFormListBean.pileDiameter.equals("")?"未知":mFormListBean.pileDiameter+"m"));
        //if (mFormListBean.holeMethod != null && !mFormListBean.holeMethod.equals("null"))
        //    mInfo.add(String.format("%s|%s","成孔方式",mFormListBean.holeMethod.equals("")?"未知":mFormListBean.holeMethod));
        switch (mFormListBean.applyMethod)
        {
            case "1":
                mInfo.add(String.format("%s|%s","报检检测方法","低应变"));
                break;
            case "2":
                mInfo.add(String.format("%s|%s","报检检测方法","高应变"));
                break;
            case "3":
                mInfo.add(String.format("%s|%s","报检检测方法","声测"));
                break;
            case "4":
                mInfo.add(String.format("%s|%s","报检检测方法","静载"));
                break;
            default:
                mInfo.add(String.format("%s|%s","报检检测方法","未知"));
                break;
        }
        mInfo.add(String.format("%s|%s","起始里程",mFormListBean.startMile.equals("")?"未知":mFormListBean.startMile));
        mInfo.add(String.format("%s|%s","结束里程",mFormListBean.endMile.equals("")?"未知":mFormListBean.endMile));
        mInfo.add(String.format("%s|%s","受力类型",mFormListBean.stressType.equals("")?"未知":mFormListBean.stressType));
        return mInfo;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        inflater.inflate(R.menu.optionfrom_menu,menu);
        super.onCreateOptionsMenu(menu,inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.Trun_Down:
                showCommitDialog();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void showCommitDialog()
    {
        final long uploadedCount = DbCenter.mHistoryDbHelper.queryLoadedCounts(mFormListBean.number);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("提交报检单");
        builder.setMessage(String.format("已上传(%s)根，确定要提交该报检单吗？",uploadedCount));
        builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                dialogInterface.dismiss();
                commitPlan(uploadedCount);
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }

    private void commitPlan(long uploadedCount)
    {
        DataAcquisition.getInstance().commitInspectionForm(
                Account.getRandomCode(),
                Account.getAccount(),
                mFormListBean.id,
                mFormListBean.number,
                (int)uploadedCount,
                "正常",
                0,
                new long[]{},
                "正常",
                getContext(),
                new DataCallBack<ResponseData>(){
                    @Override
                    public void processData(ResponseData data) {
                        super.processData(data);
                        if (data.returnCode == 0)
                        {
                            UiUtils.showToast("提交成功");
                        }
                        else if (data.returnCode == -1)
                        {
                            UiUtils.showToast("验证过期,请重新登录");
                        }
                        else
                        {
                            String message = String.format("提交异常(%s)", HttpMessage.getErrorInfo(HttpMessage.commitInspectionForm,data.returnCode));
                            UiUtils.showToast(message);
                        }
                    }
                }
                );

    }






}
